﻿--[[
   @brief    
   @author   rickonzhang
   @version  1.0
   @date     2015-04-01
--]]

--[[
SERVICE_BEGIN(Svr_GameLibrary_QueryGameInfo)
    JSONB(uint64, game_id);     -- 游戏ID
    JSONB(int, is_running);     -- 是否启动
    JSONB(int, is_cloud_game);  -- 是否是云游戏
    JSONB(int, has_cloud_game_authority);  -- 是否有游戏时长
    JSONB(string, cloud_game_running_time);  -- 游戏时长
SERVICE_END
]]--
game_menue_view =
{    
    pMenuControl = nil;
    pMenuControlDark = nil;
    pMenuControlLight = nil;
    is_show_ = false;
    shoud_show_protocal_tips_when_hide_ = false;
    current_game_id = 0;
    available_time = 0;
    has_cloud_game_prilege = false;
    server_ready = true;
    cloud_launch_game = true;
    cloud_game_desc_text = "";
    cloud_game_menu_state_info = {};

    add_sub_account_uin = 9999;
    
    INDEX_NULL = 0;
    INDEX_MULI_ZONE = 1;
    INDEX_CLOSE_GAME = 2;
    INDEX_GAME_ADD_UIN = 3;
    
    INDEX_GAME_LAUNCH_CLOUD_GAME = 4;
    INDEX_GAME_ADD_CLOUD_GAME_TIME = 5;
    INDEX_GAME_CLOUD_GAME_RUNNING_STATE = 6;

    menue_id_to_index = {};
    id_to_uin = {};
    support_fix = false;
    game_qr_infos_tbl = {};
    cloud_game_running_state_id = nil;
    cloud_game_running_state_text = nil;
    color_mode_ = MainCommon.color_mode.kDark;

    init = function()
        ierd_tgp.tpf_ui.Log("game_menue_view init.");
        ierd_tgp.tpf_ui.Require('xiaohao\\sub_account_mgr.lua');
        ierd_tgp.tpf_ui.Require("TPFLocalization.lua");
        comm_center.listen_broadcast("Svr_CloudGameState", 
                    game_menue_view.change_cloud_game_state);
        comm_center.reg_service("SvrLua_Gamemenu_QueryIsShow", 
              game_menue_view.OnGamemenuQueryIsShow);
        local main_uilib = ui_utility.uilib();
        local main_wnd_id = main_uilib:GetControlIDbyCustomID(CID_MAINFRAME_Dialog_MainFrame);
        if main_wnd_id ~= 0 then
            local const_ui_str, str_holder = U8toCUIStr("WinMenu_GameMenue");
            local id = main_uilib:GetControlIDbyPath(const_ui_str, main_wnd_id);
            game_menue_view.pMenuControlDark = tolua.cast(main_uilib:GetControlPtr(id), "Tenio::IWinMenu");
            ui_utility.setproperty_by_id(game_menue_view.pMenuControlDark:GetControlID(),
            "OnMenuLostFocus", "game_menue_view.OnMenuLostFocus");

            local const_ui_str2, str_holder2 = U8toCUIStr("WinMenu_GameLightMenue");
            id = main_uilib:GetControlIDbyPath(const_ui_str2, main_wnd_id);
            game_menue_view.pMenuControlLight = tolua.cast(main_uilib:GetControlPtr(id), "Tenio::IWinMenu");
            ui_utility.setproperty_by_id(game_menue_view.pMenuControlLight:GetControlID(),
            "OnMenuLostFocus", "game_menue_view.OnMenuLostFocus");
        end

        local color_data_config = comm_center.call_service("SvrTGPConfigMgr_GetColorMode", {});
        if color_data_config ~= nil and color_data_config.color_mode ~=nil then
            game_menue_view.color_mode_ = color_data_config.color_mode;
        end;
        game_menue_view.pMenuControl = game_menue_view.pMenuControlDark;
        if game_menue_view.color_mode_ == MainCommon.color_mode.kLight then
            game_menue_view.pMenuControl = game_menue_view.pMenuControlLight;
        end
    end;

    OnMenuLostFocus = function (uilib, menu_id)
        if game_menue_view.is_show_ == true and
            game_menue_view.shoud_show_protocal_tips_when_hide_ == true then
            local param = {show = true;};
            comm_center.call_service("SvrLua_FrameBottom_ShowProtocalTips", param);
            game_menue_view.shoud_show_protocal_tips_when_hide_ = false;
        end;
        game_menue_view.is_show_ = false;
    end;

    change_cloud_game_state = function(param)
        local cur_state = param.state;
        local show_text =  nil;
        local state_normal = false;
        if cur_state == CloudGameState.kCloudGameStateServerUnavailable then
            show_text = LoadStr("FRAME_BOTTOM_CLOUD_GAMING_SERVER_CLOSED");
        elseif cur_state == CloudGameState.kCloudGameStateUpdating then
            if param.progress <= 0 then
                show_text = LoadStr("FRAME_BOTTOM_CLOUD_GAME_CHECKING");
            else
                show_text = LoadStr("FRAME_BOTTOM_CLOUD_GAME_DOWNLOADING");
                show_text = show_text .. param.progress .. "%";
            end;
        elseif cur_state == CloudGameState.kCloudGameStateLaunching then
            show_text = LoadStr("FRAME_BOTTOM_CLOUD_GAME_LAUNCHING");
        elseif cur_state == CloudGameState.kCloudGameStateRunning then
            show_text = LoadStr("FRAME_BOTTOM_CLOUD_GAME_RUNNING");
        elseif cur_state == CloudGameState.kCloudGameStateReady 
            or cur_state == CloudGameState.kCloudGameStateNeedUpdate 
            or cur_state == CloudGameState.kCloudGameStateUpdateFailed
            or cur_state == CloudGameState.kCloudGameStateLaunchFailed then
            show_text = LoadStr("ID_TPF_FRAME_BOTTOM_GAME_MENU_LAUNCH_CLOUD_GAME");
            state_normal = true;
        end;
        local number_game_id = tonumber(param.game_id);
        if number_game_id == 0 or number_game_id == nil then
            return;
        end
        if game_menue_view.cloud_game_menu_state_info[number_game_id] == nil then
            game_menue_view.cloud_game_menu_state_info[number_game_id] = {};
        end
        if game_menue_view.cloud_game_menu_state_info[number_game_id].server_ready ~=nil and
           game_menue_view.cloud_game_menu_state_info[number_game_id].server_ready == false then
          --  优先使用server_ready的 状态,  Svr_CloudGameState的消息来的更后
               base_utility.log("game_menue_view change_cloud_game_state=" .. param.state 
                .. ", progress=" .. param.progress
                .. ", cloud_channel_id=" .. param.cloud_channel_id
                .. ", game=" .. param.game_id .. ", cur_game_id:" .. tostring(game_menue_view.current_game_id)
                .. ",cloud_game_running_state_id=" ..tostring(game_menue_view.cloud_game_running_state_id)
                .. ", state_normal = " .. tostring(state_normal)
                .. ", show_text= " .. show_text .. "this data will be filtered", true);
          return;
        end;
        game_menue_view.cloud_game_menu_state_info[number_game_id].cloud_launch_game = state_normal;
        game_menue_view.cloud_game_menu_state_info[number_game_id].cloud_game_running_state_text = show_text;
        base_utility.log("game_menue_view change_cloud_game_state=" .. param.state 
                .. ", progress=" .. param.progress
                .. ", cloud_channel_id=" .. param.cloud_channel_id
                .. ", game=" .. param.game_id .. ", cur_game_id:" .. tostring(game_menue_view.current_game_id)
                .. ",cloud_game_running_state_id=" ..tostring(game_menue_view.cloud_game_running_state_id)
                .. ", state_normal = " .. tostring(state_normal)
                .. ", show_text= " .. show_text, true);
        if number_game_id == tonumber(game_menue_view.current_game_id) and
                game_menue_view.cloud_game_running_state_id ~= nil and show_text ~=nil then
                ui_utility.setproperty_by_id(game_menue_view.cloud_game_running_state_id, "Caption", show_text);
                if state_normal == true then
                    ui_utility.setproperty_by_id(game_menue_view.cloud_game_running_state_id,
                    "Disable", "false");
                    local cloud_game_desc_text = nil;
                    if game_menue_view.cloud_game_menu_state_info[number_game_id] ~= nil and
                       game_menue_view.cloud_game_menu_state_info[number_game_id].cloud_game_desc_text ~= nil
                       then
                       cloud_game_desc_text =
                       game_menue_view.cloud_game_menu_state_info[number_game_id].cloud_game_desc_text;
                    end
                    if cloud_game_desc_text ~= nil then
                        ui_utility.setproperty_by_path(game_menue_view.cloud_game_running_state_id,
                        "Btn_Text_Sub_Mid", "Visible", "true");
                        ui_utility.setproperty_by_path(game_menue_view.cloud_game_running_state_id,
                        "Btn_Text_Sub_Mid", "Caption",
                        cloud_game_desc_text);
                    end;
                else
                    ui_utility.setproperty_by_id(game_menue_view.cloud_game_running_state_id,
                    "Disable", "true");
                end;
        end;
    end;

    OnGamemenuQueryIsShow = function()
        return game_menue_view.is_show_;
    end;
    --[[
    @brief: 输入参数为banner上菜单按钮相对屏幕的位置
    ]]
    show = function(xoffset, yoffset, width, height, game_id)    
        --乒乓效果
        if nil == game_menue_view.pMenuControl then return end

        if game_menue_view.pMenuControl ~= nil and game_menue_view.pMenuControl:IsVisible() == TRUE then
            game_menue_view.is_show_ = false;
            if game_menue_view.shoud_show_protocal_tips_when_hide_ == true then
                local param = {show = true;};
                comm_center.call_service("SvrLua_FrameBottom_ShowProtocalTips", param);
                game_menue_view.shoud_show_protocal_tips_when_hide_ = false;
            end;
            game_menue_view.pMenuControl:Show(FALSE);
            return;
        end
        local color_data_config = comm_center.call_service("SvrTGPConfigMgr_GetColorMode", {});
        if color_data_config ~= nil and color_data_config.color_mode ~=nil then
            game_menue_view.color_mode_ = color_data_config.color_mode;
        end;
        local style_path_text = "Common.xml/Menu/BottomBarLightMenuStyle";
        if game_menue_view.color_mode_ == MainCommon.color_mode.kDark then
            style_path_text = "Common.xml/Menu/BottomBarDarkMenuStyle";
        end;
        ui_utility.setproperty_by_id(game_menue_view.pMenuControl:GetControlID(),
        "StylePath",  style_path_text);
        --根据游戏设置菜单项
        game_menue_view.current_game_id = game_id;
        base_utility.log("[game_menue_view]show  init_menue_item game_id:" .. tostring(game_id));
        game_menue_view.cloud_game_running_state_id = nil;
        local init_menu = game_menue_view.init_menue_item();
        if init_menu == false then
            base_utility.log("[game_menue_view]show  init_menue_item error! ");
            return;
        end;

        --获取主窗口的位置
        local main_uilib = ui_utility.uilib();
        local main_wnd_id = main_uilib:GetControlIDbyCustomID(CID_MAINFRAME_Dialog_MainFrame);
        local main_wnd = main_uilib:GetControlPtr(main_wnd_id);
        local main_wnd_rc = main_wnd:GetRectEx(TRUE,TRUE);

        local rc = game_menue_view.pMenuControl:GetRectEx(TRUE,TRUE);
        local w = rc.right - rc.left;
        local h = rc.bottom - rc.top;
        -- 获取系统工作区域大小：
        local tpf_ui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        local sys_param = ierd_tgp.tpf_ui.SysParams:new();
        tpf_ui:get_sys_params(sys_param);
        local work_area_height = sys_param.work_area_height;
        sys_param:delete();
        -- 菜单显示的位置
        rc.left = xoffset - main_wnd_rc.left - w + width;
        rc.right = rc.left + w;
        local menu_yoffset = yoffset -h - 14;
        rc.top = menu_yoffset - main_wnd_rc.top;
        rc.bottom = rc.top + h;

        game_menue_view.shoud_show_protocal_tips_when_hide_ =
            comm_center.call_service("SvrLua_FrameBottom_QueryIsProtocalTipsShow", {});

        local show_protocol_param = {show = false;};
        comm_center.call_service("SvrLua_FrameBottom_ShowProtocalTips", show_protocol_param);

        game_menue_view.pMenuControl:MoveEx(rc,TRUE);
        game_menue_view.is_show_ = true;
        game_menue_view.pMenuControl:Show(TRUE);
    end;

    -- 隐藏菜单栏
    hide = function()
        game_menue_view.cloud_game_running_state_id = nil;
        if game_menue_view.pMenuControl ~= nil then
            game_menue_view.is_show_ = false;
            game_menue_view.pMenuControl:Show(FALSE);
        end
        if game_menue_view.shoud_show_protocal_tips_when_hide_ == true then
            local param = {show = true;};
            comm_center.call_service("SvrLua_FrameBottom_ShowProtocalTips", param);
            game_menue_view.shoud_show_protocal_tips_when_hide_ = false;
        end;
    end;

    -- 初始化菜单栏item
    init_menue_item = function()
        if game_menue_view.pMenuControlDark ~= nil then
            game_menue_view.pMenuControlDark:RemoveAll();
        end;
        if game_menue_view.pMenuControlLight ~= nil then
            game_menue_view.pMenuControlLight:RemoveAll();
        end;

        game_menue_view.pMenuControl = game_menue_view.pMenuControlDark;
        if game_menue_view.color_mode_ == MainCommon.color_mode.kLight then
            game_menue_view.pMenuControl = game_menue_view.pMenuControlLight;
        end
        game_menue_view.menue_id_to_index = {};

        -- 设置要显示的menu
        local param = { game_id = game_menue_view.current_game_id};
        local query_ret = comm_center.call_service("Svr_GameLibrary_QueryGameLaunchInfo", param);
        local game_info =  nil;
        if query_ret == nil or query_ret.game_info_list == nil then
             base_utility.log("[game_menue_view]init_menue_item  error! no game info");
             return false;
        end;
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            local game_info_list = ierd_tgp.game_library.GameMgrInfoList:new();
            game_lib:get_game_mgr_info(game_info_list);
        
            for i = 0, game_info_list.count - 1 do
              if game_info_list.game_mgr_info[i].id == game_menue_view.current_game_id then
                game_menue_view.support_fix = game_info_list.game_mgr_info[i].support_fix;
                break;
              end;
            end;
            game_info_list:delete();
        end;

        game_info = base_utility.json_to_table(query_ret.game_info_list);
       --[[ game_info.is_running = false;
        game_info.has_cloud_game_authority = true;
        game_info.is_cloud_game = true;]]--
        game_menue_view.available_time = game_info.available_time;
        local number_game_id = tonumber(game_menue_view.current_game_id);
        local menu_state_info =
            game_menue_view.cloud_game_menu_state_info[number_game_id];
        if menu_state_info ~= nil then
            if menu_state_info.cloud_launch_game ~= nil then
                game_menue_view.cloud_launch_game = menu_state_info.cloud_launch_game;
                base_utility.log("[game_menue_view]init_menue_item back up value  game_id:"
                .. tostring(game_menue_view.current_game_id)
                .. ", cloud_launch_game:" .. tostring(game_menue_view.cloud_launch_game));
            end;
            if menu_state_info.cloud_game_running_state_text ~= nil then
                game_menue_view.cloud_game_running_state_text =
                    menu_state_info.cloud_game_running_state_text;
                base_utility.log("[game_menue_view]init_menue_item back up value  game_id:"
                .. tostring(game_menue_view.current_game_id)
                .. ", cloud_game_running_state_text:"
                .. tostring(game_menue_view.cloud_game_running_state_texte));
            end;
        end;
        local cloud_game_running_state_text_tmp = "";
        if game_menue_view.cloud_game_running_state_text ~= nil then
            cloud_game_running_state_text_tmp = game_menue_view.cloud_game_running_state_text;
        end;
        base_utility.log("[game_menue_view]init_menue_item  game_id:"
            .. tostring(game_menue_view.current_game_id)
            .. ", cloud_launch_game:" .. tostring(game_menue_view.cloud_launch_game)
            .. ", cloud_game_running_state_text:" .. cloud_game_running_state_text_tmp);
        if game_menue_view.cloud_game_running_state_text == nil then
            game_menue_view.cloud_game_running_state_text = LoadStr("ID_TPF_FRAME_BOTTOM_GAME_MENU_LAUNCH_CLOUD_GAME");
        end;
        local is_local_game_running = game_info.is_local_game_running;
        game_menue_view.cloud_game_desc_text = game_info.cloud_game_running_time;
        if game_info.is_running == false then
            game_menue_view.set_client_not_run_game_menue(game_info);
        elseif game_info.is_local_game_running == true then
            game_menue_view.set_running_client_game_menue(game_info);
        else
            game_menue_view.set_running_cloud_game_menue(game_info);
        end;
        return true;
    end;

    -- 设置游戏运行时菜单栏
    set_client_not_run_game_menue = function(game_info)
        local main_uilib = ui_utility.uilib();
        local menu_item_count = -1;
        
        -- 先得到一共有多少个item
        local total_item = 4;
        if game_info.has_cloud_game_authority == false then
            total_item = 2;
        end;
        local get_cloud_game_info_param = {game_id = game_menue_view.current_game_id};
        local cloud_game_cfg_info = comm_center.call_service("Svr_CloudGame_GetCloudGameInfo",
              get_cloud_game_info_param);
        local str_cloud_game = LoadStr("ID_TPF_FRAME_BOTTOM_GAME_MENU_CLOUD_GAME");

        menu_item_count = menu_item_count + 1;
        base_utility.log("[game_menue_view]set_client_not_run_game_menue, game_id:"
        .. tostring(game_menue_view.current_game_id) .. ", cloud_provider_working:"
        ..tostring(cloud_game_cfg_info.cloud_provider_working) .. ",server_ready:"
        ..tostring(cloud_game_cfg_info.server_ready));
        local str_launch_cloud_game = game_menue_view.cloud_game_running_state_text;
        if cloud_game_cfg_info ~= nil and (cloud_game_cfg_info.cloud_provider_working == false  or
           cloud_game_cfg_info.server_ready == false) then
            str_launch_cloud_game = LoadStr("FRAME_BOTTOM_CLOUD_GAMING_SERVER_CLOSED");
            game_menue_view.server_ready = false;
        elseif game_info.could_launch_provider == false then
        --  多渠道时，渠道状态正常的没有时长，有时长的渠道又在维护中
            str_launch_cloud_game = LoadStr("FRAME_BOTTOM_CLOUD_GAMING_SERVER_CLOSED");
            game_menue_view.server_ready = false;
        else
            game_menue_view.server_ready = true;
        end;

        local number_game_id = tonumber(game_menue_view.current_game_id);
        if game_menue_view.cloud_game_menu_state_info[number_game_id] == nil then
            game_menue_view.cloud_game_menu_state_info[number_game_id] = {};
        end
        game_menue_view.cloud_game_menu_state_info[number_game_id].server_ready =
                    game_menue_view.server_ready;

        base_utility.log("[game_menue_view]set_client_not_run_game_menue, game_id:"
        .. tostring(game_menue_view.current_game_id) .. ", cloud_provider_working:"
        ..tostring(cloud_game_cfg_info.cloud_provider_working) .. ",server_ready:"
        ..tostring(cloud_game_cfg_info.server_ready).. ",str_launch_cloud_game:"
        .. str_launch_cloud_game);

        local desc_text = game_info.cloud_game_running_time;
        if game_menue_view.server_ready == false then
            desc_text = "";
        end;

        game_menue_view.cloud_game_menu_state_info[number_game_id].cloud_game_desc_text = desc_text;

        game_menue_view.cloud_game_running_state_id =
            game_menue_view.add_normal_menue_item(str_launch_cloud_game,
            game_menue_view.INDEX_GAME_LAUNCH_CLOUD_GAME, menu_item_count,
            total_item, true, desc_text);

        if game_menue_view.server_ready == false then
            total_item = 4;
        end;
        
        if (game_menue_view.cloud_launch_game == false) or
            game_menue_view.server_ready == false then
            ui_utility.setproperty_by_id(game_menue_view.cloud_game_running_state_id,
                    "Disable", "true");
        else
            ui_utility.setproperty_by_id(game_menue_view.cloud_game_running_state_id,
                    "Disable", "false");
        end;
        game_menue_view.has_cloud_game_prilege = game_info.has_cloud_game_authority;
        if game_info.has_cloud_game_authority == true or game_menue_view.server_ready == false then
            menu_item_count = menu_item_count + 1;
            game_menue_view.add_line_menue_item();
            menu_item_count = menu_item_count + 1;
            local str_add_cloud_game_time = LoadStr("ID_TPF_FRAME_BOTTOM_GAME_MENU_LAUNCH_ADD_CLOUD_GAME_TIME");
            game_menue_view.add_normal_menue_item(str_add_cloud_game_time,
            game_menue_view.INDEX_GAME_ADD_CLOUD_GAME_TIME, menu_item_count, total_item, true, "");
        end;
    end;

    -- 设置游戏运行时菜单栏
    set_running_client_game_menue = function(game_info)
        local main_uilib = ui_utility.uilib();
        local menu_item_count = -1;
        
        -- 先得到一共有多少个item
        local total_item = 0;
       
        if base_utility.support_mul_instances(game_menue_view.current_game_id) then
            total_item = total_item + 1;
        end;
        -- 单号多开
        local base_info = banner_model.get_game_base_info(game_menue_view.current_game_id);
        if base_info ~= nil and base_info.support_more_zone == true then
            total_item = total_item + 1; 
        end
        -- 每个游戏都有本地应有
        total_item = total_item + 1;
        local local_app = LoadStr("ID_TPF_FRAME_BOTTOM_GAME_MENU_LOCAL_APP");
        --小号列表,支持的游戏设置小号列表
        if base_utility.support_mul_instances(game_menue_view.current_game_id) then
            local str_choose_acc = LoadStr(ID_CHOOSE_ACC);
            menu_item_count = menu_item_count + 1;
            game_menue_view.add_normal_menue_item(str_choose_acc, game_menue_view.INDEX_SUB_MENUE,
            menu_item_count, total_item, true, "");
            game_menue_view.add_uin_list_menue_item(menu_item_count);
        end;

        -- 单号多开
        if base_info ~= nil and base_info.support_more_zone == true then
            local str_mult_zone = LoadStr(ID_MULI_ZONE);
            menu_item_count = menu_item_count + 1;
            game_menue_view.add_normal_menue_item(str_mult_zone, game_menue_view.INDEX_MULI_ZONE,
            menu_item_count, total_item, true, "");
        end;

        menu_item_count = menu_item_count + 1;
        local str_close_game = LoadStr("ID_TPF_FRAME_BOTTOM_GAME_MENU_CLOSE_GAME");
        game_menue_view.add_normal_menue_item(str_close_game, game_menue_view.INDEX_CLOSE_GAME,
        menu_item_count, total_item, true, "");
    end;

    -- 设置云游戏运行时菜单栏
    set_running_cloud_game_menue = function(game_info)
        base_utility.log("[game_menue_view]set_running_cloud_game_menue call");
        local main_uilib = ui_utility.uilib();
        local menu_item_count = -1;

        -- 先得到一共有多少个item
        local total_item = 4;
        local str_cloud_game = LoadStr("ID_TPF_FRAME_BOTTOM_GAME_MENU_CLOUD_GAME");
        menu_item_count = menu_item_count + 1;
        local str_launch_cloud_game = LoadStr("ID_TPF_FRAME_BOTTOM_GAME_MENU_LAUNCH_CLOUD_GAME");
        if game_menue_view.cloud_game_running_state_text == nil then
            game_menue_view.cloud_game_running_state_text = str_launch_cloud_game;
        end;
        game_menue_view.cloud_game_running_state_id =
        game_menue_view.add_normal_menue_item(game_menue_view.cloud_game_running_state_text,
        game_menue_view.INDEX_GAME_CLOUD_GAME_RUNNING_STATE, menu_item_count, total_item, true, "");
        if game_menue_view.cloud_launch_game == false then
            ui_utility.setproperty_by_id(game_menue_view.cloud_game_running_state_id,
                    "Disable", "true");
        else
            ui_utility.setproperty_by_id(game_menue_view.cloud_game_running_state_id,
                    "Disable", "false");
        end;

        menu_item_count = menu_item_count + 1;
        game_menue_view.add_line_menue_item();

        local str_add_cloud_game_time = LoadStr("ID_TPF_FRAME_BOTTOM_GAME_MENU_LAUNCH_ADD_CLOUD_GAME_TIME");
        menu_item_count = menu_item_count + 1;
        game_menue_view.add_normal_menue_item(str_add_cloud_game_time,
        game_menue_view.INDEX_GAME_ADD_CLOUD_GAME_TIME, menu_item_count, total_item, true, "");
    end;

    set_btn_state = function(item_index, total_item, id, text_str, is_sub_item, desc_str)
        local button_text;
        if item_index == 0 and total_item == 1 then
            ui_utility.setproperty_by_path(id, "Btn_Text_UpDown", "Visible", "true");
            ui_utility.setproperty_by_path(id, "Btn_Text_Up", "Visible", "false");
            ui_utility.setproperty_by_path(id, "Btn_Text_Mid", "Visible", "false");
            ui_utility.setproperty_by_path(id, "Btn_Text_Down", "Visible", "false");
            -- 设置显示文本
            ui_utility.setproperty_by_path(id, "Btn_Text_UpDown", "Caption", text_str);
            -- 获得btnid 返回
            button_text = "Btn_Text_UpDown";
        elseif item_index == 0 and total_item > 1 then
            ui_utility.setproperty_by_path(id, "Btn_Text_UpDown", "Visible", "false");
            ui_utility.setproperty_by_path(id, "Btn_Text_Up", "Visible", "true");
            ui_utility.setproperty_by_path(id, "Btn_Text_Mid", "Visible", "false");
            ui_utility.setproperty_by_path(id, "Btn_Text_Down", "Visible", "false");
            ui_utility.setproperty_by_path(id, "Btn_Text_Up\\Btn_Text_Sub_Up", "Visible", "true");
            -- 设置显示文本
            ui_utility.setproperty_by_path(id, "Btn_Text_Up", "Caption", text_str);
            ui_utility.setproperty_by_path(id, "Btn_Text_Up\\Btn_Text_Sub_Up", "Caption", desc_str);
            -- 获得btnid 返回
            button_text = "Btn_Text_Up";
        elseif item_index > 0 and item_index < (total_item - 1) then
            ui_utility.setproperty_by_path(id, "Btn_Text_UpDown", "Visible", "false");
            ui_utility.setproperty_by_path(id, "Btn_Text_Up", "Visible", "false");
            ui_utility.setproperty_by_path(id, "Btn_Text_Mid", "Visible", "true");
            ui_utility.setproperty_by_path(id, "Btn_Text_Mid\\Btn_Text_Sub_Mid", "Visible", "true");
            ui_utility.setproperty_by_path(id, "Btn_Text_Down", "Visible", "false");
            -- 设置显示文本
            ui_utility.setproperty_by_path(id, "Btn_Text_Mid", "Caption", text_str);
            ui_utility.setproperty_by_path(id, "Btn_Text_Mid\\Btn_Text_Sub_Mid", "Caption", desc_str);
            -- 获得btnid 返回
            button_text = "Btn_Text_Mid";
        elseif item_index >0 and item_index == (total_item - 1) then
            ui_utility.setproperty_by_path(id, "Btn_Text_UpDown", "Visible", "false");
            ui_utility.setproperty_by_path(id, "Btn_Text_Up", "Visible", "false");
            ui_utility.setproperty_by_path(id, "Btn_Text_Mid", "Visible", "false");
            ui_utility.setproperty_by_path(id, "Btn_Text_Down", "Visible", "true");
            -- 设置显示文本
            ui_utility.setproperty_by_path(id, "Btn_Text_Down", "Caption", text_str);
            -- 获得btnid 返回
            button_text = "Btn_Text_Down";
        end;
        if is_sub_item == false then
            ui_utility.setproperty_by_path(id, button_text, "Disable", "true");
        end;
        --[[if is_sub_item == false then
            ui_utility.setproperty_by_path(id, button_text, "TextOffsetX", "27");
        else
        ui_utility.setproperty_by_path(id, button_text, "TextOffsetX", "37");
        end;]]--
        return ui_utility.get_ctrl_id_by_path(id, button_text);
    end;

    add_blank_menue_item = function()
        local tpf_file = "GameMenueItemBlankForBottomBar.tpf";
        if game_menue_view.color_mode_ == MainCommon.color_mode.kDark then
            tpf_file = "game_menu\\dark_mode\\GameMenueItemBlankForBottomBar.tpf";
        end;
        local const_ui_str, str_holder = U8toCUIStr(tpf_file);
        local id = game_menue_view.pMenuControl:AppendItem(const_ui_str);
        if id ~= 0 then
        else
            base_utility.log("[game_menue_view]add_blank_menue_item error! ")
        end
        return id;
    end;
    
     add_line_menue_item = function()
        local tpf_file = "GameMenueItemLine.tpf";
        if game_menue_view.color_mode_ == MainCommon.color_mode.kDark then
            tpf_file = "game_menu\\dark_mode\\GameMenueItemLine.tpf";
        end;
        local const_ui_str, str_holder = U8toCUIStr(tpf_file);
        local id = game_menue_view.pMenuControl:AppendItem(const_ui_str);
        if id ~= 0 then
        else
            base_utility.log("[game_menue_view]add_line_menue_item error! ")
        end
        return id;
    end;

    -- 添加文字菜单项
    --  is_sub_item 同一个菜单里的缩进菜单ITEM
    --  desc_str  一个菜单项里文本显示 后又有一个文本，如"剩余XXX分钟"
    add_normal_menue_item = function(text_str, index_type, item_index, total_item, is_sub_item, desc_str)
        local tpf_file = "GameMenueItemNormal.tpf";
        if game_menue_view.color_mode_ == MainCommon.color_mode.kDark then
            tpf_file = "game_menu\\dark_mode\\GameMenueItemNormal.tpf";
        end;
        if is_sub_item then
            tpf_file = "GameMenueSubItemNormal.tpf";
            if game_menue_view.color_mode_ == MainCommon.color_mode.kDark then
                tpf_file = "game_menu\\dark_mode\\GameMenueSubItemNormal.tpf";
            end;
        end;
        local const_ui_str, str_holder = U8toCUIStr(tpf_file);
        local id = game_menue_view.pMenuControl:AppendItem(const_ui_str);
        if id ~= 0 then
            local control_id = game_menue_view.set_btn_state(item_index, total_item, id, text_str, is_sub_item, desc_str);
            game_menue_view.menue_id_to_index[id] = index_type;
            id = control_id;
        else
            base_utility.log("[game_menue_view]add_normal_menue_item error! ")
        end
        return id;
    end;

    -- 添加小号列表
    add_uin_list_menue_item = function(index)
        local tpf_file = "GameMenueSubMenue.tpf";
        if game_menue_view.color_mode_ == MainCommon.color_mode.kDark then
            tpf_file = "game_menu\\dark_mode\\GameMenueSubMenue.tpf";
        end;
        local const_ui_str, str_holder = U8toCUIStr(tpf_file);
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local p_sub_menue = nil;

        -- local rc = game_menue_view.pMenuControl:GetRectEx(TRUE,TRUE);
        -- if rc.left > 0 and (rc.left - 160 < 0) then
        --     p_sub_menue = game_menue_view.pMenuControl:AppendSubMenu(index, const_ui_str, Tenio.SMSP_RIGHT);
        -- else
            
        -- end;

        p_sub_menue = game_menue_view.pMenuControl:AppendSubMenu(index, const_ui_str, Tenio.SMSP_LEFT);
        if p_sub_menue == nil then
            base_utility.log("[game_menue_view]add_uin_list_menue_item error! index:" .. tostring(index));
        end;
        game_menue_view.id_to_uin = {};
        local uin_list = game_menue_control.get_sub_account_list();
        -- 加1是因为"添加帐号"按钮
        local uin_count = uin_list.count + 1;
        for i = 0, uin_list.count - 1 do 
            local qq = uin_list.uins[i];
            game_menue_view.add_single_uin_item(p_sub_menue, true, i, uin_count, qq);
        end;

        -- 添加添加小号按钮
        game_menue_view.add_single_uin_item(p_sub_menue, false, uin_count - 1, uin_count);
    end;

    -- 添加单个uin元素
    add_single_uin_item = function(p_sub_menue, is_uin, cur_index, total_num, uin)
        if not p_sub_menue then
            return;
        end
        local main_uilib = ui_utility.uilib();
        local item_content = "";
        if is_uin == true then
            if uin ~= nil and uin ~= 0 then
                item_content = tostring(uin);
            end;
        else
            item_content = LoadStr(ID_ADD_SUB_QQ);
        end;
        local tpf_file = "GameMenueItemUin.tpf";
        if game_menue_view.color_mode_ == MainCommon.color_mode.kDark then
            tpf_file = "game_menu\\dark_mode\\GameMenueItemUin.tpf";
        end;
        local const_ui_str, str_holder = U8toCUIStr(tpf_file);
        local id = p_sub_menue:AppendItem(const_ui_str);

        if id ~= 0 then
            local Btn_id = game_menue_view.set_btn_state(cur_index, total_num, id, item_content, true, "");
            local const_ui_str2, str_holder2 = U8toCUIStr("Btn_Delete");
            local delete_btn_id = main_uilib:GetControlIDbyPath(const_ui_str2, id);

            if is_uin == true then
                game_menue_view.id_to_uin[Btn_id] = uin;
                game_menue_view.id_to_uin[delete_btn_id] = uin;
            else
                -- 将添加小号功能按照QQ号来保存，在响应时单独区分开
                game_menue_view.id_to_uin[Btn_id] = game_menue_view.add_sub_account_uin;
                ui_utility.setproperty_by_id(delete_btn_id, "Visible", "false");
            end;
            
            
            -- const_ui_str, str_holder = U8toCUIStr("Ctn_RunFlag");
            -- local flag_id = main_uilib:GetControlIDbyPath(const_ui_str, id);
            -- local flag_ctrl = tolua.cast(main_uilib:GetControlPtr(flag_id), "Tenio::IUIContainer");
            -- flag_ctrl:Show(FALSE); 
        else
            base_utility.log("[game_menue_view]add_normal_menue_item error! ")
        end
    end;

    -- 点击小号多开子item响应
    run_uin = function(uilib, dwControlId)
        if game_menue_view.id_to_uin[dwControlId] ~= nil then
            if game_menue_view.id_to_uin[dwControlId] == game_menue_view.add_sub_account_uin then
                -- 点击的是添加小号
                game_menue_view.add_uin();

            else
                -- 点击的是QQ号
                sub_account_mgr.login_sub_account(game_menue_view.current_game_id,
                            game_menue_view.id_to_uin[dwControlId],
                            ierd_tgp.game_library.LAUNCH_OP_FROM_MAIN_WND);
            end;
            
        else
            base_utility.log("[game_menue_view] run with uin error!!!");
        end
    end;

    -- 删除小号
    delete_uin = function(uilib, dwControlId)
        if game_menue_view.id_to_uin[dwControlId] ~= nil then
            sub_account_mgr.del_sub_account(game_menue_view.id_to_uin[dwControlId]);
        else
            base_utility.log("[game_menue_view] delete_uin uin error!!!");
        end
    end;

    -- 添加帐号按钮响应
    add_uin = function()
        local uin_list = game_menue_control.get_sub_account_list();
        if uin_list.count >= 3 then
            local param = {
                first_line = LoadStr(ID_TOMUCH_SUB_ACCOUNT_TIP_FIRST),
                second_line = LoadStr(ID_TOMUCH_SUB_ACCOUNT_TIP_SECOND),
                icon = "ONEIMAGE:bottom_bar\\tips_icon\\red_icon.png;Stretch;0;",
            }
            event_center.send_event(sub_account_login_event.SUB_ACCOUNT_SHOW_TIPS, 
                base_utility.table_to_json(param));
        else
            local info = {game_id = game_menue_view.current_game_id};
            event_center.send_event(sub_account_login_event.SUB_ACCOUNT_ADD_ACCOUNT, 
                base_utility.table_to_json(info));
        end;
        return true;
    end;

    show_sub_account_list = function(item_id)
        ierd_tgp.tpf_ui.Require("game_menue\\sub_account_view.lua");
        sub_account_view.show(game_menue_view.current_game_id);
        return true;
    end;

    -- 点击列表时的响应函数
    on_click_item = function(uilib, id, item_id, item_index)
        local index_type = game_menue_view.menue_id_to_index[item_id];
        if index_type == game_menue_view.INDEX_MULI_ZONE then
            -- 多区双开功能实现
            game_menue_view.on_mult_zone_open(item_id);
        elseif index_type == game_menue_view.INDEX_CLOSE_GAME then
            -- 关闭游戏功能实现
            game_menue_view.on_close_game(item_id);
        elseif index_type == game_menue_view.INDEX_GAME_LAUNCH_CLOUD_GAME then
            -- 拉起云游戏功能实现
            base_utility.log("[game_menue_view] click launch cloud game server ready:"
            .. tostring(game_menue_view.server_ready) .. ", has_cloud_game_prilege:"
            .. tostring(game_menue_view.has_cloud_game_prilege) .. ", cloud_launch_game:"
            .. tostring(game_menue_view.cloud_launch_game));
            if game_menue_view.server_ready == true then
                if game_menue_view.has_cloud_game_prilege == true and game_menue_view.cloud_launch_game then
                    game_menue_view.on_launch_cloud_game(item_id);
                else
                    game_menue_view.on_add_cloud_game_time(item_id);
                end
            else
              base_utility.log("[game_menue_view]server not ready!");
            end;
        elseif index_type == game_menue_view.INDEX_GAME_ADD_CLOUD_GAME_TIME then
            -- 关闭游戏功能实现
             base_utility.log("[game_menue_view] click add  cloud time");
            game_menue_view.on_add_cloud_game_time(item_id);
        else
            base_utility.log("[game_menue_view]index type is valid type!!!");
        end;
        return true;
    end;

    on_select_item = function(uilib, id, item_id, item_index)
        return 0;
    end;

    -- 多区双开实现
    on_mult_zone_open = function (item_id)
        base_utility.log("[game_menue_view]mult zone open");
        local game_id = game_menue_view.current_game_id;
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib then
            base_utility.log("[game_menue_view]LaunchGameTcls, game_id is %d", game_id);
            game_lib:LaunchGameTcls(game_id);
        else
            base_utility.log("[game_menue_view]open mult zone, get game_lib failed");
            return;
        end;
    end;

    -- 关闭游戏功能实现
    on_close_game = function(item_id)
        base_utility.log("[game_menue_view] close game");
        game_menue_control.excute_game_act(GameActions.kStopGame,
                    game_menue_view.current_game_id, 0);
    end;

    --[[

    SERVICE_BEGIN(Svr_CloudGamePurchase_Request)
JSONB(uint64, game_id);
JSONB(string, parameter);
JSONB(int, width);
JSONB(int, height);
SERVICE_END

    SERVICE_BEGIN(SvrLua_CloudGame_Launch_Request)
JSONB(uint64, game_id);
SERVICE_END
    ]]--
    on_add_cloud_game_time = function(Item_id)
       base_utility.log("[game_menue_view] on_add_cloud_game_time");
       local param = {
       game_id = game_menue_view.current_game_id;
       parameter = "#/?game_id=" .. tostring(game_menue_view.current_game_id);
       width = 660;
       height = 500;
       };
       comm_center.call_service("SvrLua_CloudGame_Purchase_Request", param);
    end;

    on_launch_cloud_game = function(item_id)
        base_utility.log("[game_menue_view] on_launch_cloud_game, time:"
        .. tostring(game_menue_view.available_time));
        local param = {game_id = game_menue_view.current_game_id};
        comm_center.call_service("SvrLua_CloudGame_Launch_Request", param);
    end;
};

game_menue_view.init();
